#include "swpprojectdocument.h"

#include <coremimetypes.h>
#include <mimetypemanager.h>

using namespace core;
using namespace swp;

SwpProjectDocument::SwpProjectDocument(const QString& name,
    const core::MimeType& mimeType) : IContainerDocument(),
        m_name(name),
        m_mimeType(mimeType),
        m_isDirty(false),
        m_documents(),
        m_data()
{
}

SwpProjectDocument::~SwpProjectDocument()
{

}

void SwpProjectDocument::setValid(bool valid)
{
    // do nothing
}

bool SwpProjectDocument::isValid() const
{
    return true;
}

bool SwpProjectDocument::isFuture() const
{
    return false;
}

bool SwpProjectDocument::isLinked() const
{
    return false;
}

bool SwpProjectDocument::isContainer() const
{
    return true;
}

bool SwpProjectDocument::isDirty() const
{
    return m_isDirty;
}

QString SwpProjectDocument::name() const
{
    return m_name;
}

MimeType SwpProjectDocument::mimeType() const
{
    return m_mimeType;
}

QSharedPointer<IContainerDocument> SwpProjectDocument::container() const
{
    return QSharedPointer<IContainerDocument>(0);
}

void SwpProjectDocument::setDirty(bool dirty)
{
    if (m_isDirty != dirty)
    {
        m_isDirty = dirty;
        emit dirtyChanged(m_isDirty);
    }
}

void SwpProjectDocument::setFuture(bool future)
{
    // do nothing, it is always future
}

void SwpProjectDocument::setContainer(
    const QSharedPointer<IContainerDocument>& container)
{
    // do nothing, it is top level
}

void SwpProjectDocument::rename(const QString& name)
{
    if (m_name != name)
    {
        m_name = name;
        emit nameChanged();
    }
}

IDocumentData* SwpProjectDocument::data() const
{
    return &m_data;
}

core::DocumentCategories SwpProjectDocument::categories() const
{
    QList<QString> ret;

    ret.append(tr("Activity diagrams"));
    ret.append(tr("Class diagrams"));
    ret.append(tr("Simulation cases"));
    ret.append(tr("Reports"));

    return ret;
}

QList<QSharedPointer<IDocument> > SwpProjectDocument::documents(
    const QString& categoryName) const
{
    QList<QSharedPointer<IDocument> > ret;

    if (m_documents.contains(categoryName))
    {
        ret = m_documents[categoryName];
    }

    return ret;
}

QList<MimeType> SwpProjectDocument::categoryMimeTypes(
        const QString& categoryName) const
{
    QList<MimeType> ret;

    if (categoryName == tr("Activity diagrams"))
    {
        ret.append(MimeTypeManager::getInstance()->mimeType(
            ACTIVITY_DIAGRAM_MIMETYPE));
    }
    else if (categoryName == tr("Class diagrams"))
    {
        ret.append(MimeTypeManager::getInstance()->mimeType(
            CLASS_DIAGRAM_MIMETYPE));
    }
    else if (categoryName == tr("Simulation cases"))
    {
        ret.append(MimeTypeManager::getInstance()->mimeType(
            SIM_CASE_MIMETYPE));
    }
    else if (categoryName == tr("Reports"))
    {
        ret.append(MimeTypeManager::getInstance()->mimeType(
            REPORT_MIMETYPE));
    }
    else
    {
        // do nothing
    }

    return ret;
}

QString SwpProjectDocument::categoryFor(
    const QSharedPointer<IDocument>& document) const
{
    QString ret;

    foreach (const QString& category, m_documents.keys())
    {
        if (m_documents[category].contains(document))
        {
            ret = category;
            break;
        }
    }

    return ret;
}

bool SwpProjectDocument::isTopLevel() const
{
    return true;
}

void SwpProjectDocument::addDocument(const QString& categoryName,
    const QSharedPointer<IDocument>& document)
{
    if (!m_documents[categoryName].contains(document))
    {
        m_documents[categoryName].append(document);
    }
}

void SwpProjectDocument::removeDocument(const QString& categoryName,
    const QSharedPointer<IDocument>& document)
{
    if (m_documents.contains(categoryName) &&
        m_documents[categoryName].contains(document))
    {
        m_documents[categoryName].removeAll(document);
    }
}

void SwpProjectDocument::removeDocument(
    const QSharedPointer<IDocument>& document)
{
    foreach (const QString& categories, m_documents.keys())
    {
        if (m_documents[categories].contains(document))
        {
            m_documents[categories].removeAll(document);
            break;
        }
    }
}

bool SwpProjectDocument::isChildrenDirty() const
{
    bool ret = false;

    foreach (const QString& category, m_documents.keys())
    {
        foreach (const QSharedPointer<IDocument>& document,
            m_documents[category])
        {
            ret = ret || document->isDirty();
        }
    }

    return ret;
}

void SwpProjectDocument::linksUpdated()
{
    setDirty(true);
}
